package eworldpacs.ui;

import leadtools.dicom.DicomDataSet;
import leadtools.dicom.DicomElement;
import leadtools.dicom.DicomIod;
import leadtools.dicom.DicomIodTable;
import leadtools.dicom.DicomModule;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.widget.TextView;

public class CreateTreeAsyncTask extends AsyncTask<FillLeadTreeArgs, Integer, LeadTreeView> {

   OnLeadTreeBuildCompletedListener _listener;
   Object _caller;
   
   @Override
   protected LeadTreeView doInBackground(FillLeadTreeArgs... params) {
      try
      {
         assert params.length == 1;
         FillLeadTreeArgs args = params[0];
         _caller = args.getCaller();
         LeadTreeNode rootNode = new LeadTreeNode(null);
         LeadTreeView treeView = args.getTreeView();
         switch(args.getMode())
         {
            case FillLeadTreeArgs.LIST_MODE:
               rootNode = fillTreeListMode((Context)_caller,args.getDataSet(),rootNode);
               break;
            case FillLeadTreeArgs.MODULE_MODE:
               rootNode = fillTreeModuleMode((Context)_caller,args.getDataSet(),rootNode);
               break;
         }
         treeView.setRootLevelNode(rootNode);		
         return treeView;
      }
      catch (Exception ex)
      {
         ex.printStackTrace();
      }
      return null;
   }
   
   public void setCompletedListener(OnLeadTreeBuildCompletedListener listener){
      _listener = listener;
   }
   
   @Override
   protected void onPostExecute(LeadTreeView treeView) {
        if (_listener != null)
         _listener.onBuildCompleted(treeView, _caller);
    }
   
   private LeadTreeNode fillTreeListMode(Context context, DicomDataSet ds, LeadTreeNode rootNode){
      DicomElement startingElement = ds.getFirstElement(null, true, true);
      return fillTree(context, ds, startingElement, rootNode, true);
   }
   
   private LeadTreeNode fillTree(Context context, DicomDataSet ds, DicomElement startingElement, LeadTreeNode rootNode, boolean allBranches){
      
      LeadTreeNode currentParentNode = rootNode;
      DicomElement currentElement = startingElement;
      
      LeadTreeNode previousParentNode = null;
      DicomElement previousElement = null;
      while (currentElement != null){
         LeadTreeNode currentNode = new LeadTreeNode(currentParentNode);
         String currentDisplayText = DicomDemoUtils.ElementToString(currentElement);
         TextView currentDisplayTextView = new TextView(context);
         currentDisplayTextView.setTextColor(Color.WHITE);
         currentDisplayTextView.setTextSize(24);
         currentDisplayTextView.setText(currentDisplayText);
         //currentDisplayTextView.setMaxWidth(screenWidth);
         currentDisplayTextView.setMaxLines(1);
         currentDisplayTextView.setEllipsize(TextUtils.TruncateAt.END);
         if (ds.isVolatileElement(currentElement)){
            currentDisplayTextView.setTextColor(Color.RED);
           }
         currentNode.setView(currentDisplayTextView);
         currentNode.setTag(currentElement);
         currentParentNode.getChildren().add(currentNode);
           
           DicomElement childElement = ds.getChildElement(currentElement, true);
           if (childElement != null){
            previousParentNode = currentParentNode;
            previousElement = currentElement;
              currentParentNode = currentNode;
              currentElement = childElement;
           } else {
            if ((!allBranches && (currentElement == startingElement)))
               break;
            else
               currentElement = ds.getNextElement(currentElement, true, true);
            while ((currentElement == null) && (previousParentNode != null)){
               if (!allBranches && currentParentNode.getParent() == rootNode){
                  currentElement = null;
                  break;
               }
               currentParentNode = previousParentNode;
               currentElement = ds.getNextElement(previousElement, true, true);
               Object previousParentTag = previousParentNode.getTag();
               if (previousParentTag instanceof DicomElement)
                  previousElement = (DicomElement)previousParentTag;
               else
                  previousElement = null;
               previousParentNode = currentParentNode.getParent();
            }
           }   
      }
      return rootNode;
   }
   
   private LeadTreeNode fillTreeModuleMode(Context context, DicomDataSet ds, LeadTreeNode rootNode){
      int moduleCount = ds.getModuleCount();
      
      for (int x = 0; x < moduleCount; x++)
      {
         DicomModule module = ds.findModuleByIndex(x);
         LeadTreeNode node = new LeadTreeNode(rootNode);
         DicomIod iod = DicomIodTable.getInstance().findModule(ds.getInformationClass(), module.getType());
         String displayText = iod.getName();
         TextView displayTextView = new TextView(context);
         displayTextView.setTextColor(Color.WHITE);
         displayTextView.setTextSize(24);
         displayTextView.setText(displayText);
         node.setView(displayTextView);
         node.setTag(module);
         rootNode.getChildren().add(node);
         for (DicomElement element : module.getElements())
         {
          fillTree(context, ds, element, node, false);
         }
      }
      return rootNode;
   }
}
